// Implementation of circular lists, using singly linked elements.
// (c) 1998, 2001 duane a. bailey

package Calculadora;

/**
 * An implementation of lists using circularly linked elements, 
 * similar to that of {@link java.util.LinkedList java.util.LinkedList}.
 * @version $Id: SinglyLinkedList.java,v 4.1 2000/12/29 02:39:16 bailey Exp bailey $
 * @author, 2001 duane a. bailey
 * @see SinglyLinkedList
 * @see DoublyLinkedList
 */
public class CircularList<E> extends AbstractList<E>
{
    /**
     * A reference to tail of list.  tail points to head.
     */
    protected SinglyLinkedListElement tail;
    /**
     * Number of elements within circular list.
     */
    protected int count;

    /**
     * Construct an empty circular list.
     *
     * @pre constructs a new circular list
     */
    public CircularList()
    {
	tail = null;
	count = 0;
    }
    /**
     * Add an element to head of list.
     *
     * @pre value non-null
     * @post adds element to head of list
     * 
     * @param value  value added to head of list.	
     */
    @Override
    public void addFirst(E value)
    {
	SinglyLinkedListElement temp =
	    new SinglyLinkedListElement(value);
	if (tail == null) { // first value added
	    tail = temp;
	    tail.setNext(tail);
	} else { // element exists in list
	    temp.setNext(tail.next());
	    tail.setNext(temp);
	}
	count++;
   }
    /**
     * Determine if a list is empty.
     *
     * @pre !isEmpty()
     * @post returns value at head of list
     * 
     * @return True if there are no elements within list.
     */
    @Override
    public E getFirst()
    {
	return (E) tail.next().value();
    }

    /**
     * Remove a value from head of list.
     *
     * @pre !isEmpty()
     * @post returns and removes value from head of list
     * 
     * @return value removed.
     */
    @Override
    public E removeFirst()
    {
	SinglyLinkedListElement temp = tail.next(); // ie. head of list
	if (tail == tail.next()) {
	    tail = null;
	} else {
	    tail.setNext(temp.next());
	    temp.setNext(null);	// helps clean things up; temp is free
	}
	count--;
	return (E) temp.value();
    }

    /**
     * Determine size of list.
     *
     * @post returns number of elements in list
     * 
     * @return  number of elements in list.
     */
    @Override
    public int size()
    {
	return count;
    }

    /**
     * Get value at location i.
     *
     * @pre 0 <= i < size()
     * @post returns object found at that location
     *
     * @param i position of value to be retrieved.
     * @return value retrieved from location i (returns null if i invalid)
     */
   
}


